package com.dengqi.mymodule.activity.rxandroid;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dengqi.mymodule.BaseActivity;
import com.dengqi.mymodule.R;
import com.jakewharton.rxbinding2.view.RxView;
import com.module.imagelib.activityresult.RxActivityResult;
import com.module.imagelib.glide.ImageLoader;
import com.module.imagelib.glide.strategy.GlideStrategy;
import com.module.imagelib.picture.PictureResult;
import com.module.imagelib.picture.RxPicture;
import com.module.imagelib.picture.nine.NineFragment;

import java.util.concurrent.TimeUnit;

import io.reactivex.ObservableSource;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;


/**
 * rx使用
 */
public class RxAndroidActivity extends BaseActivity {

    Disposable disposable;
    Disposable disposable2;
    ImageView iv_test;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rxandroid);
        iv_test = findViewById(R.id.iv_test);
        ImageLoader.getInstance().setImageLoader(new GlideStrategy(this));

        NineFragment.createNineFragment(this, R.id.framelayout, 9);

        disposable2 = RxView.clicks(findViewById(R.id.btn2))
                .throttleFirst(500, TimeUnit.MILLISECONDS)//500毫秒之内只取一个点击事件，防抖操作
                .map(new Function<Object, Intent>() {
                    @Override
                    public Intent apply(Object o) throws Exception {
                        return new Intent(RxAndroidActivity.this, RxSecondActivity.class);
                    }
                })
                .flatMap(new Function<Intent, ObservableSource<Intent>>() {
                    @Override
                    public ObservableSource<Intent> apply(Intent intent) throws Exception {
                        return RxActivityResult.with(RxAndroidActivity.this).startIntent(intent);
                    }
                })
                .map(new Function<Intent, String>() {

                    @Override
                    public String apply(Intent intent) throws Exception {
                        return intent.getStringExtra("name");
                    }
                })
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        ((TextView) findViewById(R.id.text)).setText(s);
                    }
                });

    }

    public void onBtn1(View view) {
        disposable = RxActivityResult.with(this)
                .startIntent(new Intent(this, RxSecondActivity.class))
                .map(new Function<Intent, String>() {
                    @Override
                    public String apply(Intent intent) throws Exception {
                        return intent.getStringExtra("name");
                    }
                })
                .subscribe(s -> ((TextView) findViewById(R.id.text)).setText(s));


    }

    public void onBtn3(View view) {
        disposable = RxPicture.with(this)
                .startPhoto()
                .subscribe(new Consumer<PictureResult>() {
                    @Override
                    public void accept(PictureResult result) throws Exception {
                        if (result.isOk()) {
                            //getCompressPath()压缩路径
                            //getCutPath()裁剪路径
                            ImageLoader.getInstance().load(result.getPaths().get(0)).circle().into(iv_test);
                        }
                    }
                });
    }

    public void onBtn4(View view) {
        disposable = RxPicture.with(this)
                .startAlbum(true)
                .subscribe(new Consumer<PictureResult>() {
                    @Override
                    public void accept(PictureResult result) throws Exception {
                        if (result.isOk()) {
                            //getPath()原图
                            //getCompressPath()压缩路径
                            //getCutPath()裁剪路径
                            ImageLoader.getInstance().load(result.getPaths().get(0)).into(iv_test);
                        }
                    }
                });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (disposable != null) {
            disposable.dispose();
        }
        if (disposable2 != null) {
            disposable2.dispose();
        }

    }
}
